import {
  queryTestTradeList, saveTestTrade, updateTestTrade,
  deleteTestTrade, queryTestTradeDetail, saveTestQuotation, saveTestApplication, saveTestWorkOrder, saveTestReport
} from '@/api/transaction'
import { getToken } from '@/utils/auth'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  introduction: '',
  roles: []
}

const actions = {
  queryTestTradeList({ commit }, params) {
    return new Promise((resolve, reject) => {
      queryTestTradeList(params).then(response => {
        const { data } = response
        if (!data) {
          reject('获取失败，请稍后再试~')
        }
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  saveTestTrade({ commit }, params) {
    return new Promise((resolve, reject) => {
      saveTestTrade(params).then(response => {
        const { data } = response
        if (!data) {
          reject('保存失败，请稍后再试~')
        }
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  updateTestTrade({ commit }, params) {
    return new Promise((resolve, reject) => {
      updateTestTrade(params).then(response => {
        const { data } = response
        if (!data) {
          reject('更新失败，请稍后再试~')
        }
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  deleteTestTrade({ commit }, params) {
    return new Promise((resolve, reject) => {
      deleteTestTrade(params).then(response => {
        const { data } = response
        if (!data) {
          reject('删除失败，请稍后再试~')
        }
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  queryTestTradeDetail({ commit }, params) {
    return new Promise((resolve, reject) => {
      queryTestTradeDetail(params).then(response => {
        const { data } = response
        if (!data) {
          reject('获取失败，请稍后再试~')
        }
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  saveTestQuotation({ commit }, params) {
    return new Promise((resolve, reject) => {
      saveTestQuotation(params).then(response => {
        const { data } = response
        if (!data) {
          reject('保存失败，请稍后再试~')
        }
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  saveTestApplication({ commit }, params) {
    return new Promise((resolve, reject) => {
      saveTestApplication(params).then(response => {
        const { data } = response
        if (!data) {
          reject('保存失败，请稍后再试~')
        }
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  saveTestWorkOrder({ commit }, params) {
    return new Promise((resolve, reject) => {
      saveTestWorkOrder(params).then(response => {
        const { data } = response
        if (!data) {
          reject('保存失败，请稍后再试~')
        }
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  saveTestReport({ commit }, params) {
    return new Promise((resolve, reject) => {
      saveTestReport(params).then(response => {
        const { data } = response
        if (!data) {
          reject('保存失败，请稍后再试~')
        }
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  }

}
export default {
  namespaced: true,
  state,
  actions
}
